package application;

import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import preprocessing.AreaNormalizer;
import preprocessing.AvgAggregator;
import preprocessing.Normalizer;

import utils.config.Config;
import data.CityArea;
import data.MatrixTime;

public class ErlangCheckIn {
	
	private Calendar START;
	private Calendar END;
	private CityArea AREA;
	private String BASE_OUT_DIR;
	private boolean SPACE_NORM;
	private boolean TIME_NORM;
	private final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
	
	public static void main(String[] args) throws Exception {
		ErlangCheckIn ec = new ErlangCheckIn();
		ec.run(Calendar.MONDAY); 
		ec.run(Calendar.TUESDAY); 
		ec.run(Calendar.WEDNESDAY);  
		ec.run(Calendar.THURSDAY); 
		ec.run(Calendar.FRIDAY); 
		ec.run(Calendar.SATURDAY); 
		ec.run(Calendar.SUNDAY);	
	}
	
	public ErlangCheckIn() {
		Config config = new Config(this.getClass().getSimpleName()+".ser"); 
		
		START = (Calendar)config.get("START");
		END = (Calendar)config.get("END");
		
		AREA = new CityArea((String)config.get("AREA_NAME"), 
				   (double[][])config.get("AREA_BBOX"),
				   (String[])config.get("BASE_IN_DIRS"));
		
		System.out.println(AREA.getRadius());
		
		BASE_OUT_DIR = (String)config.get("BASE_OUT_DIR");
		SPACE_NORM = (Boolean)config.get("SPACE_NORM");
		TIME_NORM = (Boolean)config.get("TIME_NORM");
	}
	
	public void run(int DAY) throws Exception {
		
		
		final int[] DAYS = new int[]{
			DAY
		};
		
		String S_DAYS = "";
		for(int i=0; i<DAYS.length;i++) {
				if(DAYS[i] == Calendar.MONDAY) S_DAYS = S_DAYS + "mon-";
				if(DAYS[i] == Calendar.TUESDAY) S_DAYS = S_DAYS + "tue-";
				if(DAYS[i] == Calendar.WEDNESDAY) S_DAYS = S_DAYS + "wed-";
				if(DAYS[i] == Calendar.THURSDAY) S_DAYS = S_DAYS + "thu-";
				if(DAYS[i] == Calendar.FRIDAY) S_DAYS = S_DAYS + "fri-";
				if(DAYS[i] == Calendar.SATURDAY) S_DAYS = S_DAYS + "sat-";
				if(DAYS[i] == Calendar.SUNDAY) S_DAYS = S_DAYS + "sun-";
		}
		
		
		
		final String OUT_DIR = FORMATTER.format(START.getTime())+"-"+FORMATTER.format(END.getTime())+
											  "-"+S_DAYS+
											  "-"+(SPACE_NORM ? "sn":"")+"-"+(TIME_NORM ? "tn":"")+
											  "-"+AREA.getName()+"/";
		
		
	
		run(START,END,AREA,BASE_OUT_DIR,DAYS,SPACE_NORM,TIME_NORM,OUT_DIR);
		System.out.println("Done");
		
		for(int i=0; i<10;i++) {
			Toolkit.getDefaultToolkit().beep();
			Thread.sleep(1000);
		}
	}
	
	public void run(Calendar START, Calendar END, CityArea AREA, String BASE_OUT_DIR,
			int[] DAYS, boolean SPACE_NORM, boolean TIME_NORM, String OUT_DIR) throws Exception {
		System.out.println("Start...");
		
		List<MatrixTime> ets = new ArrayList<MatrixTime>();
		
		for(String dir: AREA.getBase_in_dirs())
			ErlangMap.parseDir(dir,ets, START, END, AREA, DAYS);
		
		
		System.out.println("Reading Complete...");
		
		List<MatrixTime> day_avg = AvgAggregator.day_avg(ets);
		
	
		if(SPACE_NORM)
			Normalizer.space_normalize(day_avg);
		if(TIME_NORM)
			Normalizer.time_normalize(day_avg);
		
		Normalizer.normalize01(day_avg);
		
		new File(BASE_OUT_DIR+OUT_DIR).mkdir();		
		PrintStream out = new PrintStream(new FileOutputStream(new File(BASE_OUT_DIR+OUT_DIR+"/avg_erlang.csv")));
		
		AreaNormalizer.avg_area(day_avg, out);
		
		out.close();
	}

}
